#version 420 core

// Input
in vec2 UV;

//>>>>Task2
in vec3 Normal;
//>>>>TASK3
in vec3 LightDir_vcs;
in vec3 ViewDir_vcs;
//in vec3 NormalDir_vcs;//传入视图坐标系下的法线方向
in vec4 VertPos_vcs;//传入顶点位置（vcs）
//>>>>Task4
in mat3 TBN;
//>>>>Task5
in float heightValue;//传入高度

// Output
out vec3 color;

//Uniforms
layout (binding=1) uniform sampler2D DiffuseTextureSampler;
layout (binding=2) uniform sampler2D RoughnessTextureSampler;// Task3
//TASK5
layout (binding=4) uniform sampler2D SnowTextureSampler;
layout (binding=5) uniform sampler2D SnowRoughnessTextureSampler;
layout (binding=7) uniform sampler2D GrassTextureSampler;
layout (binding=8) uniform sampler2D GrassRoughnessTextureSampler;

//TASK4
uniform sampler2D normalTexture;

//TASK5
uniform sampler2D snow_normalTexture;//雪地法线贴图
uniform sampler2D grass_normalTexture;//草地法线贴图

uniform vec3 LightPos;//传入光源位置


void main(){
	//原来的代码：outputting a colour from the texture
	//color = texture( DiffuseTextureSampler,vec2(UV.x,UV.y)).rgb;

	//>>>>>>>>>TASK2	
	// 输出法线向量的绝对值作为颜色
	//color = vec3(abs(Normal.x), abs(Normal.y), abs(Normal.z));

	//>>>>TASK3
	// 新建光照颜色,光照强度
	vec3 LightColor = vec3(1, 1, 1);
	float LightPower = 1.0;
	// 由于需要纹理平铺而不是拉伸, 所以调整UV坐标, 并设置重复平铺的次数
	int repeatTimes = 10;
	// 使用fract函数确保UV坐标在[0,1]范围内
	vec2 tiledUV = fract(UV * repeatTimes);
	// 通过灰度图获取粗糙度Roughness的值
	float roughness = texture(RoughnessTextureSampler,vec2(tiledUV.x,tiledUV.y)).r;
	// 计算光泽度
	float Shininess = clamp((2 / (pow(roughness, 4) + 1e-2)) - 2, 0, 500.0f); 
	//float Shininess = 1/roughness;

	
	//>>>>TASK4
	
	// 获取法线贴图当前值的颜色
	// 从法线贴图中采样法线值
	vec3 normalMap = texture(normalTexture, tiledUV ).rgb;
	// 将法线值从[0, 1]范围映射到[-1, 1]
	normalMap = normalMap * 2.0 - 1.0;
	// 将法线贴图的法线转换到viewModel中
	vec3 NormalDir_vcs = normalize(TBN * normalMap);


	//>>>>TASK3:ROCK

	//实现Blinn-Phong着色

	//计算漫反射项
	//float diff = clamp(dot(normalDir_VCS, lightDir_VCS), 0, 1);
	float diffusefactor = max(dot(NormalDir_vcs, LightDir_vcs), 0.0);
	vec3 diffuseColor = texture(DiffuseTextureSampler,vec2(tiledUV.x,tiledUV.y)).rgb;
	//vec3 diffuse = diffusefactor * diffuseColor;
    vec3 diffuse = diffusefactor * diffuseColor * LightColor * LightPower;
	
	//计算镜面反射项
	//半程向量
	vec3 halfwayDir = normalize(LightDir_vcs + ViewDir_vcs);
    //float spec = clamp(pow(clamp(dot(normalDir_VCS, halfwayDir), 0, 1), shininess),0 ,1);
	float specularfactor = pow(max(dot(NormalDir_vcs, halfwayDir), 0.0), Shininess);
	//定义镜面反射Specular颜色
	vec3 specularColor = vec3(0.2, 0.2, 0.2);
    vec3 specular = specularfactor * specularColor * LightColor * LightPower;

	// 计算环境光
	vec3 ambient = vec3(0.1,0.1,0.1) * diffuseColor;
	// 计算最终颜色
	vec3 rockColor = ambient + diffuse + specular;


	//>>>>TASK5:snow and grass

	//计算雪的粗糙度
	float snow_roughness = texture(SnowRoughnessTextureSampler,vec2(tiledUV.x,tiledUV.y)).r;
	// 计算光泽度
	float snow_Shininess = clamp((2 / (pow(snow_roughness, 4) + 1e-2)) - 2, 0, 500.0f); 
	//获取雪地法线贴图的当前颜色
	vec3 snow_normalMap = texture(snow_normalTexture, tiledUV).rgb;
	snow_normalMap = snow_normalMap * 2.0 - 1.0;
	vec3 snow_NormalDir_vcs = normalize(TBN * snow_normalMap);
	//计算雪的漫反射
	vec3 snow_diffuseColor = texture(SnowTextureSampler,vec2(tiledUV.x,tiledUV.y)).rgb;
	float snow_diffusefactor = max(dot(snow_NormalDir_vcs, LightDir_vcs), 0.0);
	vec3 snow_diffuse = snow_diffuseColor * snow_diffusefactor * LightColor * LightPower;
	//计算雪的镜面反射
	float snow_specularfactor = pow(max(dot(snow_NormalDir_vcs, halfwayDir), 0.0), snow_Shininess);
	vec3 snow_specular = snow_specularfactor * specularColor * LightColor * LightPower;
	//计算雪地的环境反射
	vec3 snow_ambient = vec3(0.1,0.1,0.1) * snow_diffuseColor;
	vec3 snowColor = snow_diffuse + snow_specular + snow_ambient;

	//计算草的粗糙度
	float grass_roughness = texture(GrassRoughnessTextureSampler,vec2(tiledUV.x,tiledUV.y)).r;
	// 计算光泽度
	float grass_Shininess = clamp((2 / (pow(grass_roughness, 4) + 1e-2)) - 2, 0, 500.0f); 
	//获取草法线贴图的当前颜色
	vec3 grass_normalMap = texture(grass_normalTexture, tiledUV).rgb;
	grass_normalMap = grass_normalMap * 2.0 - 1.0;
	vec3 grass_NormalDir_vcs = normalize(TBN * grass_normalMap);
	//计算雪的漫反射
	vec3 grass_diffuseColor = texture(GrassTextureSampler,vec2(tiledUV.x,tiledUV.y)).rgb;
	float grass_diffusefactor = max(dot(grass_NormalDir_vcs, LightDir_vcs), 0.0);
	vec3 grass_diffuse = grass_diffuseColor * grass_diffusefactor * LightColor * LightPower;
	//计算雪的镜面反射
	float grass_specularfactor = pow(max(dot(grass_NormalDir_vcs, halfwayDir), 0.0), grass_Shininess);
	vec3 grass_specular = grass_specularfactor * specularColor * LightColor * LightPower;
	//计算雪地的环境反射
	vec3 grass_ambient = vec3(0.1,0.1,0.1) * grass_diffuseColor;
	vec3 grassColor = grass_diffuse + grass_specular + grass_ambient;


	float glassend = 1.0;
	float rockstart = 1.5;
	float rockEnd = 2.0;
	float snowStart = 2.2;
	float snowEnd = 2.5;
	
	if(heightValue < glassend){
    	//在草地上
    	color = grassColor;
	}
	else if(heightValue > glassend && heightValue < rockstart){
    	//在草地与岩石过度上
    	color = mix(grassColor, rockColor, smoothstep(glassend, rockstart, heightValue));
	}
	else if(heightValue > rockstart && heightValue < rockEnd){
    	//在岩石上
    	color = rockColor;
	}
	else if(heightValue > rockEnd && heightValue < snowStart){
    	//在岩石与雪地过度上
    	color = mix(rockColor, snowColor, smoothstep(rockEnd, snowStart, heightValue));
	}
	else{
    	//在雪地上
    	color = snowColor;
	}
	
}